#!/usr/bin/env python
#coding=utf-8
import json

#使用前请先安装相关的库
# pip install aliyun-python-sdk-core # 安装阿里云SDK核心库
# pip install aliyun-python-sdk-ecs # 安装管理ECS的库
from aliyunsdkcore.client import AcsClient
from aliyunsdkcore.request import CommonRequest


#client = AcsClient('AccessKey ID', 'AccessKey Secret', 'cn-hangzhou')
#'AccessKey ID', 'AccessKey Secret', 为在阿里云申请的，网址：https://usercenter.console.aliyun.com/#/manage/ak
from rest_framework.response import Response
# 个人阿里云
# client = AcsClient('LTAI4G9vhsZaHqdFoHYxAUQ2', 'yjWXr8rTaP3cid5oEdWSJZHMhpLte9', 'cn-hangzhou')
# SignName = "综合信息平台"
# Template_validcode = "SMS_205466243" #验证码10分钟有效模板
# Template_password = "SMS_205456328"  #动态密码10分钟有效模板
# 公司阿里云
client = AcsClient('LTAI4FyZM4zScykrwFJtnRsj', 'jMlI8VCnbgNBDtkAdtGeyffzcIhQIv', 'cn-hangzhou')
SignName = "贵州铁通"
Template_password = "SMS_210069164"  #动态密码10分钟有效模板
#params在官方给的示例代码中没有导致出错，具体错误和解决方案见：https://www.it610.com/article/1297045175234797568.htm


#发送验证码
def send_sms_code(PhoneNumbers,code,TemplateCode=Template_password):
    try:
        params = {
            "code": code
        }
        request = CommonRequest()

        request.set_accept_format('json')
        request.set_domain('dysmsapi.aliyuncs.com')
        request.set_method('POST')
        request.set_protocol_type('https')  # https | http
        request.set_version('2017-05-25')
        request.set_action_name('SendSms')

        request.add_query_param('RegionId', "cn-hangzhou")
        request.add_query_param('PhoneNumbers', PhoneNumbers)
        request.add_query_param('TemplateParam', json.dumps(params))
        request.add_query_param('SignName', SignName)
        request.add_query_param('TemplateCode', TemplateCode)

        response = client.do_action(request)
        res = json.loads(str(response, encoding='utf-8'))
    except Exception as e:
        print(e)
    # response = {"Message":"OK","RequestId":"0D4863A9-D79C-47BE-97DA-38C3037284D1","BizId":"408011805155632852^01","Code":"OK"}
    return res


import random

def get_code():
    code = random.sample('0123456789',6)   #随机取6位数
    code_num = ''.join(code)
    return code_num

#
# if __name__ == '__main__':
#     code_num = get_code()
#     print(code_num)


# if __name__ == '__main__':
#     res = send_sms_code('18308576111','9699')
#     print(res['Code'])